﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.Helper;
using SMS.Charts;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class ListStatService : WebControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ClientState.MemberInfo.UserType == 0 || ClientState.MemberInfo.UserType == 1 || ClientState.MemberInfo.UserType == 4)
            {
                this.BindCode();
            }
            else
                Response.Redirect("Default.aspx?module=Warning");
        }

        private void BindCode()
        {
            DataTable dt = ServiceDetailsManager.SelectAll();
            if (dt != null)
            {
                ddlCode.DataSource = dt;
                ddlCode.DataTextField = "Code1";
                ddlCode.DataValueField = "ServiceDetailID";
                ddlCode.DataBind();
                ddlCode.Items.Insert(0, new ListItem("[ Chọn mã ]", "0"));

                this.BindParam(ddlCode.SelectedValue);
            }
            else
            {
                ddlCode.DataSource = null;
                ddlCode.DataBind();
            }
        }

        private void BindParam(string Code)
        {
            DataTable dt = ServiceDetailsManager.SelectByCode(Code);
            if (dt != null)
            {
                ddlParam.DataSource = dt;
                ddlParam.DataTextField = "Param1";
                ddlParam.DataValueField = "ServiceDetailID";
                ddlParam.DataBind();
                ddlParam.Items.Insert(0, new ListItem("[ Chọn tham số ]", "0"));
            }
            else
            {
                ddlParam.DataSource = null;
                ddlParam.DataBind();
            }
        }

        protected void ddlCode_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            BindParam(ddlCode.SelectedValue + "_" + ddlParam.SelectedValue);
        }

        protected void btnStat_Click(object sender, EventArgs e)
        {
            this.BindStat(this.ddlCode.SelectedValue);
        }

        protected void BindStat(string ServiceID)
        {
            try
            {
                FusionLineChart lineChart = new FusionLineChart();

                if (ddlMsgType.SelectedValue.ToLower() == "mo")
                    lineChart.caption = "Thống kê tin đến theo mã " + ServiceID.ToUpper();
                else if (ddlMsgType.SelectedValue.ToLower() == "mt")
                    lineChart.caption = "Thống kê tin gửi đi theo mã " + ServiceID.ToUpper();
                else if (ddlMsgType.SelectedValue.ToLower() == "cdr")
                    lineChart.caption = "Thống kê tin tính phí theo mã " + ServiceID.ToUpper();

                string chartHtml = lineChart.CreateSetElement(BuildData(ServiceID), FusionLineChart.Line2D, divLineChart.ClientID, false, "", "name", "value", "text", "id", "#", true, 800, 600);
                divLineChart.InnerHtml = chartHtml;
            }
            catch { }
        }

        private DataTable BuildData(string ServiceID)
        {
            DataTable dt = null;

            if (ddlMsgType.SelectedValue.ToLower() == "mo")
                dt = MessageInManager.StatisticMsgIn_ByService(ServiceID);

            else if (this.ddlMsgType.SelectedValue.ToLower() == "mt")
                dt = MessageOutManager.StatisticMsgOut_ByService(ServiceID);

            else if (this.ddlMsgType.SelectedValue.ToLower() == "cdr")
                dt = MessageOutManager.StatisticMsgCdr_ByService(ServiceID);

            if (dt != null && dt.Rows.Count > 0)
            {
                DataTable dtData = new DataTable();

                dtData.Columns.Add("name");
                dtData.Columns.Add("value");
                dtData.Columns.Add("text");
                dtData.Columns.Add("id");

                DataRow drData = null;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    drData = dtData.NewRow();

                    drData["name"] = dt.Rows[i]["RequestTime"].ToString();
                    drData["value"] = dt.Rows[i]["RowNo"].ToString();
                    drData["text"] = "Tổng số tin : " + dt.Rows[i]["RowNo"].ToString();
                    drData["id"] = "";

                    dtData.Rows.Add(drData);
                }

                return dtData;
            }
            else
            {
                this.lblError.Text = "Hiện tại chưa có số liệu thống kê tin.";
            }
            return null;
        }
    }
}
